// pages/openRecord/openRecord.js
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        page_no: 1,
        order_type: '0',
        month: "", // 月份   
        default_date: "", // 初始化日期
        default_date1: "",
        list: [],
        start_date: '',
        end_date: '',
        showMore:false
    },
    lower(e) {
        console.log(e);
        if(!this.data.showMore){
            this.setData({
                page_no: this.data.page_no + 1
            })
            this.getData();
        }
    },
    cancelApply(e){
        let data={
            enterprise_invoice_id:e.currentTarget.dataset.id||''
        },
        that=this;
        Dialog.confirm({
            title: '取消申请',
            message: '确定取消申请？',
            asyncClose: true,
            className: 'btnWidth'
          }).then(() => {
            wx.$api.cancelInvoice(data).then(res => {
                if (res.data.code == 200) {
                    this.setData({
                        list: [],
                        page_no:1
                    })
                    that.getData();
                    wx.$Toast(res.data.msg);
                    Dialog.close();
                }
            }).catch(ret => {
                wx.$Toast(ret.msg || "服务繁忙")
            })
          }).catch(() => {
            Dialog.close();
          });
        
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        console.log(new Date())
        this.setData({
            start_date: this.getDate(new Date(),1),
            end_date: this.getDate(new Date())
        })
        console.log(this.data)
        // 默认时间
        let date = new Date();
        let year = date.getFullYear();
        let year1 = date.getFullYear()-1;
        let month = date.getMonth() + 1;
        this.setData({
            default_date: year1 + '年' + month + '月',
            default_date1: year + '年' + month + '月',
            month: month > 9 ? year + '' + month : year + '0' + month,
            list:[],
            page_no:1
        })
        console.log(this.data)
        this.getData();
    },
    replaceTime(time) {
        let date = time.replace(/-/g, "/"); //替换字符，变成标准格式 
        return new Date(Date.parse(date));
    },
    getData() {
        let startTime = this.replaceTime(this.data.start_date),
            endTime = this.replaceTime(this.data.end_date);
        console.log(startTime > endTime)
        if (startTime > endTime) {
            return wx.$Toast("开始时间大于结束时间");
        }
        let data = {
                page_no: this.data.page_no,
                order_type: this.data.order_type,
                start_date: this.data.start_date,
                end_date: this.data.end_date
            },
            that = this;
            wx.showLoading({
              title: '加载中...',
              mask:true,
              icon:'none'
            })
        wx.$api.invoiceList(data).then(res => {
            console.log(res.data)
            wx.hideLoading()
            if (res.data.code == 200) {

                if (res.data.data.list.length > 0) {
                    if (this.data.list.length > 0) {
                        that.setData({
                            list: this.data.list.concat(res.data.data.list)
                        })
                    } else {
                        that.setData({
                            list: res.data.data.list
                        })
                    }
                }else{
                    that.setData({
                        showMore:true
                    })
                }
            }

        }).catch(ret => {
            wx.hideLoading()
            wx.$Toast(ret.msg || "服务繁忙")
        })
    },
    switchTab(e) {
        console.log(e)
        if (e.currentTarget.dataset.orderType == this.data.order_type) return
        this.setData({
            order_type: e.currentTarget.dataset.ordertype,
            list:[],
            page_no:1
        })
        this.getData();
        console.log(this.data.order_type)
    },
    toDetail(e) {

        let {id}=e.currentTarget.dataset;
        wx.navigateTo({
            url: '/pages/openRecordDetail/openRecordDetail?id='+id,
        })
    },
    setPage() {
        this.setData({
            page_no: 1,
            showMore:false,
            list:[]
        })
    },
    getDate(date, type) {
        const year = type ? date.getFullYear() - 1 : date.getFullYear(),
            month = date.getMonth() + 1,
            day = date.getDate(),
            hour = date.getHours(),
            minute = date.getMinutes(),
            second = date.getSeconds();
        return year + '-' + month;
    },
    month_change(e) {
        console.log(e)
        let {
            value
        } = e.detail || {}, {
            type
        } = e.currentTarget.dataset;
        console.log(value)
        if (type == 0) {
            this.setData({
                month: value,
                default_date: parseInt(value.split('-')[0]) + '年' + parseInt(value.split('-')[1]) +'月',
                start_date: value,
            })
        } else {
            this.setData({
                month: value,
                default_date1: parseInt(value.split('-')[0]) + '年' + parseInt(value.split('-')[1]) +'月',
                end_date: value
            })
        }

        this.setPage();
        this.getData();
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})